package com.sskj.gocoinapp.ui.activity;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ImageUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.widget.RandomTextView;
import com.sskj.gocoin.utils.LanguageUtil;
import com.sskj.gocoin.R2;
import com.sskj.gocoin.utils.LanguageUtil;
import com.sskj.gocoinapp.R;
import com.sskj.gocoinapp.bean.MiningQPBean;
import com.sskj.gocoinapp.bean.Water;
import com.sskj.gocoinapp.presenter.MiningPresenter;
import com.sskj.gocoinapp.util.AppTipUtil;
import com.sskj.gocoinapp.view.WaterView;
import com.sskj.lib.RConfig;
import com.sskj.lib.RxBusCode;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.util.CommonUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


@Route(path = RConfig.APP_MINING) // 挖矿
public class MiningActivity extends BaseActivity<MiningPresenter> {

    @BindView(R.id.waterView)
    WaterView waterView;
    @BindView(R.id.trade)
    TextView tradeTv;
    @BindView(R.id.mining_yl)
    LinearLayout miningYl;
    @BindView(R.id.ll_gct)
    LinearLayout llGct;
    @BindView(R.id.title_notice)
    ImageView titleNoticeImg;
    @BindView(R.id.ll_usdt)
    LinearLayout llUsdt;
    @BindView(R.id.mining_yl_num)
    TextView miningYlNum;
    @BindView(R.id.rl_gct_num)
    RandomTextView rlGctNum;
    @BindView(R.id.rl_usdt_num)
    RandomTextView rlUsdtNum;
    @BindView(R.id.app_title)
    ImageView appTitle;
    private int range;  // 动画浮动区间
    private int baseY = 0;

    @Override
    protected int getLayoutResId() {
        return R.layout.app_mining_activity;
    }

    @Override
    public MiningPresenter getPresenter() {
        return new MiningPresenter();
    }

    @Override
    protected void initView() {
        super.initView();
        RxBus.getDefault().register(this);
        if (LanguageUtil.getAppLanguage(this).equals("zh")) {
            appTitle.setImageResource(R.mipmap.app_title);
        } else {
            appTitle.setImageResource(R.mipmap.app_title_en);
        }
        range = (int) RudenessScreenHelper.pt2px(this, 100);
        setTitle(App.INSTANCE.getString(R.string.appJavaMiningActivity1));
        waterView.setOnWaterViewClick(new WaterView.OnWaterViewClick() {
            @Override
            public void onWaterViewClick(MiningQPBean waterData, View view) {
                ClickUtil.click(view, () -> {
                    mPresenter.successQp(view, waterData.isUsdtOrGct() ? "usdt" : "gct", waterData.getId());
                });
            }
        });
        ClickUtil.click(titleNoticeImg, () -> {
            mPresenter.getNotice();
        });
        ClickUtil.click(tradeTv, () -> {
            RxBus.getDefault().send(RxBusCode.MINING_LEVEL);
            finish();
        });
        ClickUtil.click(miningYl, () -> {   // 原力明细
            ARouter.getInstance().build(RConfig.MINE_COMMON_DETAIL).withInt("type", 5).navigation();
        });
        ClickUtil.click(llGct, () -> {
            ARouter.getInstance().build(RConfig.APP_SY_RECORD)
                    .withString("type", "gct_num")
                    .navigation();
        });
        ClickUtil.click(llUsdt, () -> {
            ARouter.getInstance().build(RConfig.APP_SY_RECORD)
                    .withString("type", "usdt_num")
                    .navigation();
        });
    }

    @Override
    protected void initData() {
        super.initData();
//        startAnimator();
        mPresenter.getIcome(0);
        mPresenter.getMiningNum();
    }

    @Override
    public boolean getImmersion() {
        return false;
    }

    /**
     * 开启动画
     */
//    @SuppressLint("WrongConstant")
//    private void startAnimator() {
//        miningCenterImg.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
//            @Override
//            public void onGlobalLayout() {
//                ObjectAnimator animator = ObjectAnimator.ofFloat(miningCenterImg, "translationY", baseY, baseY + range, baseY, baseY - range, baseY);
//                animator.setDuration(8000);
//                animator.setInterpolator(new LinearInterpolator());  // 消除动画之间的间隔时间
//                animator.setRepeatCount(ValueAnimator.INFINITE);//无限循环
//                animator.setRepeatMode(ValueAnimator.INFINITE);//
//                animator.start();
//                miningCenterImg.getViewTreeObserver().removeOnGlobalLayoutListener(this);
//            }
//        });
//    }
    public void updateQp(List<MiningQPBean> list) {
        waterView.setWaters(list);
    }

    public void updateUi(MiningQPBean miningQPBean, int type) {
        miningYlNum.setText(miningQPBean.getForce() == null ? "0" : miningQPBean.getForce());
        rlGctNum.setText(miningQPBean.getGct() == null ? "0" : miningQPBean.getGct());
        rlUsdtNum.setText(miningQPBean.getUsdt() == null ? "0" : miningQPBean.getUsdt());
        rlUsdtNum.setPianyilian(RandomTextView.FIRSTF_LAST);
        rlGctNum.setPianyilian(RandomTextView.FIRSTF_LAST);
        switch (type) {
            case 0:
                rlGctNum.start();
                rlUsdtNum.start();
                break;
            case 1:
                rlGctNum.start();
                break;
            case 2:
                rlUsdtNum.start();
                break;
        }

    }

    //移除指定的waterView
    public void removeWaterView(View view) {
        waterView.removeWater(view);
    }

    // 领取失败，显示原因
    public void showDialog(String msg, int status) {
        AppTipUtil.showDialog(MiningActivity.this, new AppTipUtil.OnTipSure() {
            @Override
            public void onSure() {
                if (400 == status) {
                    RxBus.getDefault().send(RxBusCode.MINING_LEVEL);
                    finish();
                } else if (500 == status) {
                    ARouter.getInstance().build(RConfig.MINE_SHARE_MINING).navigation();
                } else {
                    ARouter.getInstance().build(RConfig.MINE_RECHARGE_MINING).navigation();
                }
            }
        }, msg);
    }

    public void showNotice(String content) {
        AppTipUtil.showTitleNotice(this, content);
    }

}
